const app = getApp();

Page({
  data: {
    userInfo: null,
    memberInfo: null,
    menuList: [
      {
        icon: '/assets/images/order.png',
        text: '我的订单',
        url: '/pages/order/list/list'
      },
      {
        icon: '/assets/images/member.png',
        text: '会员中心',
        url: '/pages/member/index/index'
      },
      {
        icon: '/assets/images/address.png',
        text: '收货地址',
        url: '/pages/address/list/list'
      },
      {
        icon: '/assets/images/service.png',
        text: '联系客服',
        type: 'contact'
      }
    ]
  },

  onShow() {
    this.loadUserInfo();
    this.loadMemberInfo();
  },

  async loadUserInfo() {
    const token = wx.getStorageSync('token');
    if (!token) {
      wx.redirectTo({
        url: '/pages/login/login'
      });
      return;
    }

    try {
      const res = await wx.request({
        url: `${app.globalData.baseUrl}/user/info/`,
        method: 'GET',
        header: {
          'Authorization': `Bearer ${token}`
        }
      });

      if (res.data.code === 200) {
        this.setData({
          userInfo: res.data.data
        });
      }
    } catch (error) {
      console.error('获取用户信息失败', error);
    }
  },

  async loadMemberInfo() {
    try {
      const res = await wx.request({
        url: `${app.globalData.baseUrl}/member/points/`,
        method: 'GET',
        header: {
          'Authorization': `Bearer ${wx.getStorageSync('token')}`
        }
      });

      if (res.data.code === 200) {
        this.setData({
          memberInfo: res.data.data
        });
      }
    } catch (error) {
      console.error('获取会员信息失败', error);
    }
  },

  handleMenuClick(e) {
    const { type, url } = e.currentTarget.dataset;
    if (type === 'contact') {
      // 打开客服会话
      return;
    }
    wx.navigateTo({ url });
  },

  handleLogout() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          wx.clearStorageSync();
          wx.redirectTo({
            url: '/pages/login/login'
          });
        }
      }
    });
  }
}); 